package com.bird.thread.base.background;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: bird
 * @Data: 2021/7/5 15:18
 * @description: 验证：发布逸出 导致的线程安全问题
 * private  修饰的属性 在外部可以进行修改
 */
public class PublicEscape {

    private Map<String, String> status;

    private Integer count;


    public PublicEscape() {
        status = new HashMap<>();
        status.put("1", "周一");
        status.put("2", "周二");
        status.put("3", "周三");
        status.put("4", "周四");
        status.put("5", "周五");
        status.put("6", "周六");
        count = 0;
    }

    public Map<String, String> getStatus() {
        return status;
    }


    // 重新new一个对象 解决发布逸出的问题 就是
    public Map<String, String> getStatusImprove() {
        return new HashMap<>(status);
    }
}

// 测试发布一个对象 在外部可以修改private 里面的属性
class TestPublishEscape {
    public static void main(String[] args) {
        PublicEscape publicEscape = new PublicEscape();
        Map<String, String> status = publicEscape.getStatus();
//        System.out.println(status.get("1"));
//        status.remove("1");
//        System.out.println(status.get("1"));

        Map<String, String> statusImprove = publicEscape.getStatusImprove();
        System.out.println(statusImprove.get("1"));
        publicEscape.getStatusImprove().remove("1");
        System.out.println(publicEscape.getStatusImprove().get("1"));
    }
}

